<?php

namespace App\Http\Requests\PC\CustomerStore\Pack;

use App\Http\Requests\ApiRequest;
use App\Models\Store\Pack;
use Illuminate\Database\Query\Builder;
use Illuminate\Validation\Rule;

/**
 * @property string customer - route param customer_id
 * @property string name
 * @property string thumb
 * @property string priority
 * @property array assets
 * @property string pack_category_id
 */
class PackStoreRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name'                 => [
                'required', 'string' , 'min:1', 'max:'.self::MAX_STRING,
//                Rule::unique((new Pack)->getTable(), 'name')->where($this->uniqueSameCustomer())
            ],
            'thumb'                => 'nullable|file',
            'pack_category_id'     => 'nullable|int|min:0',
            'assets'               => 'required|array',
            'assets.*.asset_id'    => 'required|int|min:0',
            'assets.*.index'       => 'required|int|min:0',
            'assets.*.product_ids' => 'nullable|array',
        ];
    }

    /**
     * @return \Closure
     */
    private function uniqueSameCustomer(): \Closure
    {
        return function (Builder $query) {
            return $query->where('customer_id', $this->customer);
        };
    }

    /**
     * @return array
     */
//    public function messages()
//    {
//        return [
//            'name.*'              => '整体妆容的名称必须是字符串',
//            'thumb.*'             => '整体妆容的缩率图必须是上传的图片',
//            'assets.*.asset_id.*' => '素材的id必须是数值',
//            'assets.*.index.*'    => '素材的形状必须是数值',
//            'assets.*'            => '单个素材的信息必须是数组',
//        ];
//    }
}
